﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BTGISWASECO.DataLayer;
using BTGISWASECO.BusinessEntities;
using BTGISWASECO.BusinessLayer;

public partial class PresentationLayer_DefaultTab : System.Web.UI.Page
{
    TruHongBLL bll = new TruHongBLL();
    DMHuongDongChayDAL DMHuongDongChayDal = new DMHuongDongChayDAL();
    TruHongEntities TruHongEntities = new TruHongEntities();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["DataTable"] == "")
                Response.Redirect("../default.aspx");
            TruHongEntities = bll.GetTruHongEntitiesByID();
            for (int i = DateTime.Now.Year; i >= 1990; --i)
            {
                ListItem iteml = new ListItem(i.ToString(), i.ToString());
                DListNamLD.Items.Add(iteml);
            }

            txtObjectID.Value = TruHongEntities.OBJECTID.ToString();
            txtIDTruHong.Text = TruHongEntities.IDTruHong;
            this.txtDMZ.Text = TruHongEntities.IDDMZ;
            this.txtDMA.Text = TruHongEntities.IDDMA;
            this.txtKichCo.Text = TruHongEntities.KichCo;
            this.txtKieuDang.Text = TruHongEntities.KieuDang;
            foreach (var itemlv in DMLoaiTruHongDAL.GetList())
            {
                if (TruHongEntities.LoaiTruHongText.ToString().Trim() == itemlv.MoTa.ToString().Trim())
                    TruHongEntities.LoaiTruHong = itemlv.Ma;
                ListItem iteml = new ListItem(itemlv.MoTa.ToString(), itemlv.Ma.ToString());
                DListLoaiTruHong.Items.Add(iteml);
            }
            if (TruHongEntities.LoaiTruHong != null)
                DListLoaiTruHong.SelectedValue = TruHongEntities.LoaiTruHong.ToString();

            foreach (var itemvl in DMVatLieuDAL.GetList())
            {
                if (TruHongEntities.VatLieuText.ToString().Trim() == itemvl.MoTa.ToString().Trim())
                    TruHongEntities.VatLieu = itemvl.Ma;
                ListItem iteml = new ListItem(itemvl.MoTa.ToString(), itemvl.Ma.ToString());
                DListVatLieu.Items.Add(iteml);
            }
            DListVatLieu.SelectedValue = TruHongEntities.VatLieu.ToString();

            foreach (var itemtc in DMTieuChuanTruHongDAL.GetList())
            {
                if (TruHongEntities.TieuChuanTruHongText.ToString().Trim() == itemtc.MoTa.ToString().Trim())
                    TruHongEntities.TieuChuanTruHong = itemtc.Ma;
                ListItem iteml = new ListItem(itemtc.MoTa.ToString(), itemtc.Ma.ToString());
                DListTieuChuan.Items.Add(iteml);
            }
            DListTieuChuan.SelectedValue = TruHongEntities.TieuChuanTruHong.ToString();

            foreach (var itemc in DMHieuTruCuuHoaDAL.GetList())
            {
                if (TruHongEntities.NhanHieuText.Trim() == itemc.MoTa.ToString().Trim())
                    TruHongEntities.NhanHieu = itemc.Ma;
                ListItem iteml = new ListItem(itemc.MoTa.ToString(), itemc.Ma.ToString());
                DListNhanHieu.Items.Add(iteml);
            }
            DListNhanHieu.SelectedValue = TruHongEntities.NhanHieu.ToString();

            foreach (var itemnsx in DMNhaSanXuatDAL.GetList())
            {
                if (TruHongEntities.NhaSanXuatText.ToString().Trim() == itemnsx.MoTa.ToString().Trim())
                    TruHongEntities.NhaSanXuat = itemnsx.Ma;
                ListItem iteml = new ListItem(itemnsx.MoTa.ToString(), itemnsx.Ma.ToString());
                DListNhaSX.Items.Add(iteml);
            }
            DListNhaSX.SelectedValue = TruHongEntities.NhaSanXuat.ToString();

            foreach (var itemnusx in DMNuocSanXuatDAL.GetList())
            {
                if (TruHongEntities.NuocSanXuatText.ToString().Trim() == itemnusx.MoTa.ToString().Trim())
                    TruHongEntities.NuocSanXuat = itemnusx.Ma;
                ListItem iteml = new ListItem(itemnusx.MoTa.ToString(), itemnusx.Ma.ToString());
                DListNuocSX.Items.Add(iteml);
            }
            DListNuocSX.SelectedValue = TruHongEntities.NuocSanXuat.ToString();

            DListNamLD.SelectedValue = TruHongEntities.NamLapDat.ToString();
            this.nSLMiengPhun.Text = TruHongEntities.SLMiengPhun.ToString();
            this.txtKCMiengPhun.Text = TruHongEntities.KCMiengPhun.ToString();
            this.txtMoTaViTri.Text = TruHongEntities.MoTaViTri.ToString();
            foreach (var itemnttsd in DMTinhTrangSDDAL.GetList())
            {
                if (TruHongEntities.TinhTrangText.ToString().Trim() == itemnttsd.MoTa.ToString().Trim())
                    TruHongEntities.TinhTrang = itemnttsd.Ma;
                ListItem iteml = new ListItem(itemnttsd.MoTa.ToString(), itemnttsd.Ma.ToString());
                DListTTSD.Items.Add(iteml);
            }
            DListTTSD.SelectedValue = TruHongEntities.TinhTrang.ToString();
            this.dApLucHD.Text = TruHongEntities.ApLucHD.ToString();
            foreach (var itemntldv in DMLoaiDonViDAL.GetList())
            {
                if (TruHongEntities.DonViQLText.ToString().Trim() == itemntldv.MoTa.ToString().Trim())
                    TruHongEntities.DonViQL = itemntldv.Ma;
                ListItem iteml = new ListItem(itemntldv.MoTa.ToString(), itemntldv.Ma.ToString());
                DListDonViQL.Items.Add(iteml);
            }
            DListDonViQL.SelectedValue = TruHongEntities.DonViQL.ToString();
            this.txtSoNha.Text = TruHongEntities.SoNha.ToString();
            this.txtMaDuong.Text = TruHongEntities.MaDuong.ToString();
            this.txtMaPhuong.Text = TruHongEntities.MaPhuong.ToString();
            this.txtMaQuan.Text = TruHongEntities.MaQuan.ToString();
            foreach (var itemtsh in DMThuocSoHuuDAL.GetList())
            {
                if (TruHongEntities.ThuocSoHuuText.ToString().Trim() == itemtsh.MoTa.ToString().Trim())
                    TruHongEntities.ThuocSoHuu = itemtsh.Ma;
                ListItem iteml = new ListItem(itemtsh.MoTa.ToString(), itemtsh.Ma.ToString());
                DListThuocSoHuu.Items.Add(iteml);
            }
            DListThuocSoHuu.SelectedValue = TruHongEntities.ThuocSoHuu.ToString();
            this.nSLKPSuCo.Text = TruHongEntities.SoLanSuCo.ToString();
            this.txtNgayCNC.Text = TruHongEntities.NgayCNCuoi.ToString("dd/MM/yyyy");
            this.txtNguoiCNC.Text = TruHongEntities.NguoiCNCuoi.ToString();
            this.txtMaHinhAnh.Text = TruHongEntities.IDHinhAnh;
            this.txtGhiChu.Text = TruHongEntities.GhiChu;

        }//ThuocSoHuu
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        TruHongEntities bk = new TruHongEntities();
        TruHongDAL TruHongDAL = new TruHongDAL();
        //TruHongEntities TruHongEntities = new TruHongEntities(Convert.ToInt32(DListDonViTC.SelectedValue = DListDonViTC.SelectedValue!=""?DListDonViTC.SelectedValue:"0"),
        //    Convert.ToInt32(DListDonViGS.SelectedValue = DListDonViGS.SelectedValue!=""?DListDonViGS.SelectedValue:"0"), this.txtNguoiCNC.Text, DateTime.Parse(this.txtNgayCNC.Text.ToString()), this.txtGhiChu.Text, this.txtMaHinhAnh.Text);
        TruHongEntities TruHongEntities = new TruHongEntities(Convert.ToInt32(txtObjectID.Value),txtIDTruHong.Text,
                        this.txtDMZ.Text,this.txtDMA.Text,this.txtKichCo.Text,this.txtKieuDang.Text,
                        Convert.ToInt32(DListLoaiTruHong.SelectedValue = DListLoaiTruHong.SelectedValue != "" ? DListLoaiTruHong.SelectedValue : "0"),
                        Convert.ToInt32(DListVatLieu.SelectedValue = DListVatLieu.SelectedValue != "" ? DListVatLieu.SelectedValue : "0"),
                        Convert.ToInt32(DListTieuChuan.SelectedValue = DListTieuChuan.SelectedValue != "" ? DListTieuChuan.SelectedValue : "0"),
                        Convert.ToInt32(DListNhanHieu.SelectedValue = DListNhanHieu.SelectedValue != "" ? DListNhanHieu.SelectedValue : "0"),
                        Convert.ToInt32(DListNhaSX.SelectedValue = DListNhaSX.SelectedValue != "" ? DListNhaSX.SelectedValue : "0"),
                        Convert.ToInt32(DListNuocSX.SelectedValue = DListNuocSX.SelectedValue != "" ? DListNuocSX.SelectedValue : "0"),
                        Convert.ToInt32(DListNamLD.SelectedValue),Convert.ToInt32(this.nSLMiengPhun.Text),this.txtKCMiengPhun.Text,
                        this.txtMoTaViTri.Text,Convert.ToInt32(DListTTSD.SelectedValue = DListTTSD.SelectedValue != "" ? DListTTSD.SelectedValue : "0"),
                        Convert.ToDouble(this.dApLucHD.Text),Convert.ToInt32(DListDonViQL.SelectedValue = DListDonViQL.SelectedValue != "" ? DListDonViQL.SelectedValue : "0"),
                        this.txtSoNha.Text,this.txtMaDuong.Text,this.txtMaPhuong.Text,this.txtMaQuan.Text,
                        Convert.ToInt32(DListThuocSoHuu.SelectedValue = DListThuocSoHuu.SelectedValue != "" ? DListThuocSoHuu.SelectedValue : "0"),
                        Convert.ToInt32(this.nSLKPSuCo.Text),DateTime.Parse(Request.Form.Get("txtNgayCNC")),this.txtNguoiCNC.Text,
                        this.txtMaHinhAnh.Text,this.txtGhiChu.Text);
        TruHongDAL.Update1(TruHongEntities);
        //TruHongDAL.Update(Convert.ToInt32(txtObjectID.Value), txtMaDoanOng.Text, txtDMA.Text, Convert.ToInt32(DListHDChay.SelectedValue), Convert.ToDouble(this.nChieuDai.Text));
     //   Response.Redirect("../default.aspx");
        ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "<script language='JavaScript'>alert('Cập nhật thành công'); window.location.href ='../Default.aspx';</script>");
    }
    protected void btnHuyBo_Click(object sender, EventArgs e)
    {
        Response.Redirect("../default.aspx");
    }
}